<?php
/**
 * Created by PhpStorm.
 * User: KaBuDa
 * Date: 2020/9/16
 * Time: 10:28
 */

namespace app\api\controller\v1;


use app\common\controller\Api;
use app\common\model\Collaborate;
use app\common\model\Feedback;
use app\common\model\HelpCenter;
use app\common\model\Introduce;
use app\common\model\Member;
use app\common\model\MemberMessage;
use app\common\model\Qq;
use app\common\model\Service;
use fast\Http;
use think\Db;

class Share extends Api
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    protected $appid='wx50f4b62e0401703d';
    protected $wxSecret= '56e2f463b0094119e37d8aba65f79c97';
    /**
     * 二维码扫码注册
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function share()
    {
        $model= new Member();

        $params=$this->request->request();
        $params['pid']=1;
        $params['openid']='32sdfssdfd';
        if(empty($params['pid']))  $this->error('无效的链接');
        if(!$model->where(['id'=>$params['pid']])->count(1))  $this->error('无效的用户')  ;
       $model->startTrans();
       $model->openid=$params['openid'];
       $model->pid=$params['pid'];
       $model->headimgurl=$params['headimgurl'] ?? '';
       $model->member_id=1;
       $model->createtime=time();

       if($model->save()){

          $model->where(['id'=>$params['pid']])->setInc('number');
       if(Member::where(['number'=>10,'id'=>$model->pid])->update(['number'=>0])){
           $model->where(['id'=>$params['pid']])->setInc('wish_stamps');
       }

          $model->commit();
          $this->success('ok');
       }

       $model->rollback();
       $this->error('服务器异常');
    }

    /**
     * 意见与反馈
     */
    public function feedback()
    {
        $feedback=$this->request->request();
        $model=new Feedback();
        if(empty($feedback['content'])) $this->error('内容不能为空');
        $model->content=$feedback['content'];
        $model->member_id=0;
        if($model->save()) $this->success('ok');
        $this->error('提交失败,请稍后重试');
    }


    /**
     * 帮助中心
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function help()
    {
        $this->success('',HelpCenter::all());
    }

    /**
     * 关于我们
     * @throws \think\exception\DbException
     */
    public function aboutUs()
    {
        $this->success('',Collaborate::all());
    }

    /**
     * QQ群
     * @throws \think\exception\DbException
     */
    public function qq()
    {
        $this->success('',Qq::all());
    }

    /**
     * 通知消息
     * @param int $page
     */
    public function message($page=1)
    {
       $member_id=$this->auth->id;
       $member_id=1;
       $this->success('',MemberMessage::message($member_id,$page));
    }

    public function introduce()
    {

        $this->success('',Introduce::all());
    }

    public function welogin($code=null,$uid=null)
    {
        if(!$code) $this->error('code不能为空');
        if(!$uid)  $this->error('uid不能为空');
//       $url= " https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->wxSecret}&code={$code}&grant_type=authorization_code";
//        header('location:'.$url);
        $data='';
        if(Member::where(['openid'=>$data['unionid']])->count()) $this->success('注册成功');

        $this->success('',Http::get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->wxSecret}"));
    }

    public function server()
    {
        $this->success('',Service::get());
    }

    public function appUpdate()
    {
       $version= $this->request->post('ver');
       if(!$version) $this->error('版本号不能为空');
       $data=Db::name('update')->where('status',1)->order('createtiem','desc')->find();
       if(!$data) $this->error('暂无更新版本');
       if($data['version']==$version) $this->error('暂无更新版本');
       return $this->success('',$data);
    }
}